#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: problem_handling.sh
# Author: GID5564
# Description: 某些问题的处理方法
# Created Time: 22/11/23-21:09:19
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

function handle_gvfs_udisks2(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题: dpkg: error processing package gvfs-daemons (--configure):dependency problems - leaving unconfigured
    #      Errors were encountered while processing:
    #     udisks2
    #     gvfs-daemons
    #   E: Sub-process /usr/bin/dpkg returned an error code (1)
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    # 全部强制清理
    sudo dpkg --remove --force-depends gvfs-daemons gvfs:arm64 udisks2 && sudo apt autoremove  
    # 需要的话再安装
    # sudo apt install gvfs-daemons gvfs:arm64 udisks2  
}


function handle_udisks2(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题: 
    # 正在设置 udisks2 (2.8.4-1ubuntu2) ...
    # Failed to scan devices: Permission denied
    # dpkg: 处理软件包 udisks2 (--configure)时出错：
    # 已安装 udisks2 软件包 post-installation 脚本 子进程返回错误状态 1
    # dpkg: 依赖关系问题使得 gvfs-daemons 的配置工作不能继续：
    # gvfs-daemons 依赖于 udisks2；然而：
    # 软件包 udisks2 尚未配置。

    # dpkg: 处理软件包 gvfs-daemons (--configure)时出错：
    # 依赖关系问题 - 仍未被配置
    # 在处理时有错误发生：
    # udisks2
    # gvfs-daemons
    # E: Sub-process /usr/bin/dpkg returned an error code (1)
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  
    #删除 udisks2 的残留配置信息
    sudo rm /var/lib/dpkg/info/udisks2.*
    
    #重新配置 udisks2:
    sudo dpkg --configure udisks2
    #或直接重新安装：
    #sudo apt-get install --reinstall udisks2
    
    #尝试以 root 身份重新配置包
    sudo dpkg --configure -a
}


function handle_aptitude(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题: 
    #     E: 无法将 /var/lib/aptitude/pkgstates 改名为 /var/lib/aptitude/pkgstates.old - save_selection_list (1: 不允许的操作)
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    local cur=$(pwd)
    [[ -z $(command -v aptitude) ]] && {
        install_dpkg_packages "aptitude"
    }
    aptitude
    [[ ! -s /var/lib/aptitude/pkgstates.old ]] && {
        cd /var/lib/aptitude # 切换至工作目录
        [[ -s  pkgstates.new ]] && {
            cp -f pkgstates.new pkgstates.old #备份;
            chmod 777 pkgstates.new  # 修改文件权限 ;
            mv pkgstates.new pkgstates #修改文件名
        }
        success "处理完成!"
    }
    cd ${cur}
}

function handle_matplotlib(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题:
    #     处理jupyter下matplotlib显示中文问题
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    #复制字体文件
    cp -f ${FONT_PATH}/simhei.ttf ${FONT_MATPLOTLIB}/fonts/ttf/
    
    #清理缓冲区
    rm -rf ${SYS_ROOT}/.cache/matplotlib
    
    #备份
    cp -f ${FILE_MATPLOTLIBRC} "${FILE_MATPLOTLIBRC}_bk"
    
    #修改以下三项:
    #去掉font.family: sans-serif前的注释符号"#"
    sed -i "/font.family:/s/^[# ]*//g" ${FILE_MATPLOTLIBRC}

    #font.sans-serif: SimHei ，去掉注释#，并增加SimHei
    sed -i "/font.sans-serif:/s/^[# ]*//g" ${FILE_MATPLOTLIBRC}
    sed -i "/font.sans-serif:/s/[ \t]*$/,SimHei/g" ${FILE_MATPLOTLIBRC}

    #axes.unicode_minus: False ，去掉注释，并修改True为False
    sed -i "/axes.unicode_minus:/s/^[# ]//g" ${FILE_MATPLOTLIBRC}
    sed -i "/axes.unicode_minus:/s/True/False/g" ${FILE_MATPLOTLIBRC}
        
    success "修改完成!"
}



function handle_xfce4_resolution(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题:
    #    修改xfce4屏幕分辨率为330x680
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    local ret=$(check_dpkg_installed xfce4)
    local file="/usr/local/bin/vncserver-start"
    [[ $ret -eq 1 ]] && {
        sed -i 's/1024x768/330x680/g' ${file}
    }
    unset ret file
}

function handle_del_yans1(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题:
    #    删除~yasn1目录
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    local dir="/usr/local/lib/python3.7/dist-packages"
    
    sudo rm -rf ${dir}/~yasn1
    sudo rm -rf ${dir}/~yasn1-*
    unset dir
}


function handle_python_link(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题:
    #    python --> python3 --> python3.7
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    #移除已有软连接

    sudo rm -rf /usr/bin/python3
    sudo rm -rf /usr/bin/python

    #建立新的软连接

    sudo ln -s /usr/bin/python3.7 /usr/bin/python3
    sudo ln -s /usr//python3.7 /usr/bin/python
}


function handle_sources_list(){
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #问题:
    #    国内常用的几大Debian10镜像加速源
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    local src="/etc/apt/sources.list"
    local from=$1
    
    [[ -z "${from}" ]] && {
        error "Usage: handle_sources_list <class>"
        msg "param: class:
            163 --> 网易源
            hw --> 华为源
            tc --> 腾讯源
            ustc --> 中科大源
            ali --> 阿里云
            th --> 清华源
            lzu --> 兰州大学
            sj --> 上海交大
            debian --> 官方源"
        return $INVALID_ARGUMENTS
    }
    [[ ${from} == "debian" ]] && {
        cat <<EOF > ${src} 
deb https://deb.debian.org/debian buster main contrib
deb https://deb.debian.org/debian-security/ buster/updates main contrib
deb https://deb.debian.org/debian buster-updates main contrib
EOF
}
    [[ ${from} == "163" ]] && {
        cat <<EOF > ${src} 
deb http://mirrors.163.com/debian/ buster main non-free contrib
deb http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

deb-src http://mirrors.163.com/debian/ buster main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib
EOF
    } 
    [[ ${from} == "hw" ]] && {
        cat <<EOF > ${src} 
deb https://mirrors.huaweicloud.com/debian/ buster main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ buster-updates main contrib non-free
deb https://mirrors.huaweicloud.com/debian/ buster-backports main contrib non-free
deb https://mirrors.huaweicloud.com/debian-security/ buster/updates main contrib non-free

deb-src https://mirrors.huaweicloud.com/debian/ buster main contrib non-free
deb-src https://mirrors.huaweicloud.com/debian/ buster-updates main contrib non-free
deb-src https://mirrors.huaweicloud.com/debian/ buster-backports main contrib non-free 
EOF
    } 
    
    [[ ${from} == "tc" ]] && {
        cat <<EOF > ${src} 
deb http://mirrors.cloud.tencent.com/debian/ buster main non-free contrib
deb http://mirrors.cloud.tencent.com/debian-security buster/updates main
deb http://mirrors.cloud.tencent.com/debian/ buster-updates main non-free contrib
deb http://mirrors.cloud.tencent.com/debian/ buster-backports main non-free contrib

deb-src http://mirrors.cloud.tencent.com/debian-security buster/updates main
deb-src http://mirrors.cloud.tencent.com/debian/ buster main non-free contrib
deb-src http://mirrors.cloud.tencent.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.cloud.tencent.com/debian/ buster-backports main non-free contrib
EOF
    } 
    
    [[ ${from} == "ustc" ]] && {
        cat <<EOF > ${src} 
deb https://mirrors.ustc.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free

deb-src https://mirrors.ustc.edu.cn/debian/ buster main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ buster-updates main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ buster-backports main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free
EOF
    } 
    
    [[ ${from} == "ali" ]] && {
        cat <<EOF > ${src} 
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
EOF
    } 
    
    [[ ${from} == "th" ]] && {
        cat <<EOF > ${src} 
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free

deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free
EOF
    } 
    
    [[ ${from} == "lzu" ]] && {
        cat <<EOF > ${src} 
deb http://mirror.lzu.edu.cn/debian stable main contrib non-free
deb http://mirror.lzu.edu.cn/debian stable-updates main contrib non-free
deb http://mirror.lzu.edu.cn/debian/ buster-backports main contrib non-free
deb http://mirror.lzu.edu.cn/debian-security/ buster/updates main contrib non-free

deb-src http://mirror.lzu.edu.cn/debian stable main contrib non-free
deb-src http://mirror.lzu.edu.cn/debian stable-updates main contrib non-free
deb-src http://mirror.lzu.edu.cn/debian/ buster-backports main contrib non-free
deb-src http://mirror.lzu.edu.cn/debian-security/ buster/updates main contrib non-free
EOF
    } 
    
    [[ ${from} == "sj" ]] && {
        cat <<EOF > ${src} 
deb https://mirror.sjtu.edu.cn/debian/ buster main contrib non-free
deb https://mirror.sjtu.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirror.sjtu.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirror.sjtu.edu.cn/debian-security/ buster/updates main contrib non-free

deb-src https://mirror.sjtu.edu.cn/debian/ buster-updates main contrib non-free
deb-src https://mirror.sjtu.edu.cn/debian/ buster-backports main contrib non-free
deb-src https://mirror.sjtu.edu.cn/debian/ buster main contrib non-free
deb-src https://mirror.sjtu.edu.cn/debian-security/ buster/updates main contrib non-free
EOF
    } 
}

    
    




































